package org.pi.common.gui.helper

import scala.swing._
import scala.swing.event._

/** Trait to hide and handle evil gui magic, in this case 
* mouse interactions (clicks and drags).
*/
trait MouseInteractionsImpl extends MouseInteractions {
	private var selectedX1Screen:Int=0;
	private var selectedY1Screen:Int=0;
	private var selectedX2Screen:Int=0;
	private var selectedY2Screen:Int=0;
	private var draggedSelected:Boolean=false;
	private var jcomponent : javax.swing.JComponent = null;
	
	if (this.isInstanceOf[javax.swing.JComponent]) {
		jcomponent = this.asInstanceOf[javax.swing.JComponent]
		jcomponent.addMouseListener( new java.awt.event.MouseAdapter() {
			override def mouseClicked(event: java.awt.event.MouseEvent) : Unit = { 
				if (event.getButton()==java.awt.event.MouseEvent.BUTTON3) {
					//callMenu(event.getX(), event.getY())
					//throw new Error("TODO: uncomment code above this line")
					// 
					// --> it seems this point is reached if the right mouse button is pressed.
					// since the buttonpressed-method is also called, I do not include the
					// code above.
				}
				else
				{
					selectPoint(event.getX(), event.getY(), event.getButton() )
				}
			}
			override def mousePressed(event: java.awt.event.MouseEvent) : Unit = {
				if (!draggedSelected && event.getButton()==java.awt.event.MouseEvent.BUTTON1) { 
					selectedX1Screen = event.getX()
					selectedY1Screen = event.getY()
					draggedSelected = false
				}
				else if (event.getButton()==java.awt.event.MouseEvent.BUTTON3) {
					callMenu(event.getX(), event.getY())
				}
			}			
			override def mouseReleased(event: java.awt.event.MouseEvent) : Unit = { 
				if (draggedSelected) {
					selectedX2Screen = event.getX()
					selectedY2Screen = event.getY()
					draggedSelected = false
					selectRegion( Math.min(selectedX1Screen,selectedX2Screen),Math.min(selectedY1Screen,selectedY2Screen),Math.max(selectedX1Screen,selectedX2Screen),Math.max(selectedY1Screen,selectedY2Screen) )
		
					val g = jcomponent.getGraphics().asInstanceOf[java.awt.Graphics2D];
					g.setStroke(new java.awt.BasicStroke(2.0f));
					g.setXORMode(java.awt.Color.yellow);
					drawSelection(g);
				}
			}
		});
		jcomponent.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
			override def mouseDragged(event: java.awt.event.MouseEvent) : Unit = { 
				val g = jcomponent.getGraphics().asInstanceOf[java.awt.Graphics2D];
				g.setStroke(new java.awt.BasicStroke(2.0f));
				g.setXORMode(java.awt.Color.yellow);
				if (draggedSelected) drawSelection(g);
		
				selectedX2Screen = event.getX()
				selectedY2Screen = event.getY()
				draggedSelected = true
		
				drawSelection(g);	
			}
		});
	}
	else {
		throw new Error("MouseInteractionsImpl must be attached to a jcomponent.")
	}
	
	private def drawSelection(g: Graphics2D) : Unit = {
		if(selectedX1Screen!=selectedX2Screen || selectedY1Screen!=selectedY2Screen) {
			g.drawRect(Math.min(selectedX1Screen,selectedX2Screen),Math.min(selectedY1Screen,selectedY2Screen),Math.abs(selectedX2Screen-selectedX1Screen),Math.abs(selectedY2Screen-selectedY1Screen));
		}
	}
}
